package com.beilang.base.listener;

import static org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;

import com.beilang.base.codelookup.manager.CodeLookupMgr;

/**
 * StartupListener class used to initialize and database settings
 */
public class StartupListener implements ServletContextListener {
    private static final Log log = LogFactory.getLog(StartupListener.class);
    private static boolean initiated=false;

    public void contextInitialized(ServletContextEvent event) {
        log.debug("Initializing context...");
        if( initiated ) return;
        ServletContext context = event.getServletContext();
        ApplicationContext ctx =getRequiredWebApplicationContext(context);
        CodeLookupMgr mgr=(CodeLookupMgr)ctx.getBean("codeLookupMgr");
        mgr.loadAllCodesList();
        initiated=true;
    }


    /**
     * Shutdown servlet context (currently a no-op method).
     *
     * @param servletContextEvent The servlet context event
     */
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
    }
}